<?php
/**
 * User: lsx
 * Date: 2017/6/2
 * Time: 10:10
 * Progress:0%
 */

namespace app\shop\controller;


use icontroller\admin\AController;
use icontroller\admin\shop\IShopGoodsCategory;
use logic\admin\shop\LShopGoodsCategory;
use model\ShopStore;

class ShopGoodsCategory extends AController implements IShopGoodsCategory
{
    private $logic;
    public function __construct()
    {
        parent::__construct();
        $this->logic=new LShopGoodsCategory();
    }


    public function index()
    {
        $store_id=input('store_id');
        if($store_id > 0){
            $store = $this->get_store($store_id);
        }else{
            $store = $this->get_full_store();
        }
        $this->assign('sid',$store_id);
        $this->assign('store', $store);

        $list=$this->logic->logic_index_data();
        $this->assign('list',$list);
        return view();
        // TODO: Implement index() method.
    }

    public function index_data()
    {

        $param=array(
            'search'=>input('search'),
            'limit'=>input('limit'),
            'offset'=>input('offset'),
            'sort'=>input('sort'),
            'order'=>input('order'),
            'store'=>input('store'),
            'status'=>input('status'),
        );
        $data=$this->logic->index_data($param);
        return $data;
    }

    public function add()
    {
        $store_id=input('sid');
        if($store_id > 0){
            $store = $this->get_store($store_id);
        }else{
            $store = $this->get_full_store();
        }
        $this->assign('sid',$store_id);
        $this->assign('store', $store);
        return view();
    }

    public function insert(){
        $param=array(
            'name'=>input('name'),
            'sort'=>input('sort'),
            'flag_use'=>input('flag_use'),
            'icon'=>input('icon'),
            'store_id'=>input('store_id'),
            'parent_id'=>input('parent_id',0)
        );
        return $this->logic->logic_insert($param);
    }

    public function edit(){
        $goods_num = input('num');
        $store_id=input('sid');
        if($store_id > 0){
            $store = $this->get_store($store_id);
        }else{
            $store = $this->get_full_store();
        }

        $this->assign('sid',$store_id);

        $id=input('id');
        $data=$this->logic->logic_edit($id);
        $data['num']=$goods_num;
        $this->assign('item',$data);



        $this->assign('store', $store);
        return view();
    }

    public function update()
    {
        $param=array(
            'id'=>input('id'),
            'name'=>input('name'),
            'sort'=>input('sort'),
            'store_id'=>input('store_id'),
            'flag_use'=>input('flag_use'),
            'icon'=>input('icon'),
            'parent_id'=>input('parent_id',0)
        );
        return $this->logic->logic_update($param);
    }

    public function delete()
    {
        $param = array(
            'id' => input('id'),
            'store_id' => input('store_id')
        );

        return  $this->logic->logic_delete($param);
    }

    public function cate_tree_data(){
        $data=$this->logic->logic_cate_treeview_data();
        return $data;
    }

    // <editor-fold defaultstate="collapsed" desc="获取商家">
    public function get_store($id)
    {
        $store = new ShopStore();
        $data = $store->where('id',$id)->field('id,name as text')->select();
        return array(
            'total' => count($data),
            'rows' => $data
        );
    }
    public function get_full_store()
    {
        $store = new ShopStore();
        $total = $store->count();
        $data = $store->field('id,name as text')->select();
        return array(
            'total' => $total,
            'rows' => $data
        );
    }
    // </editor-fold>
}